<?php

/**
 * 身份证后6位，最后一位X以0代替
 *
 */
function idCardCatOut($idCard)
{
    $str_strat = substr($idCard, -6, 5);
    $str_end = substr($idCard, -1);
    if ($str_end == 'x' || $str_end == 'X') {
        $str_end = 0;
    }
    $str = $str_strat . $str_end;
    return $str;
}

function idCardHide($idCard)
{
    return substr($idCard, 0, 6) . str_repeat("*", 4) . substr($idCard, -4);
}

function phoneHide($phone)
{
    $pattern = "/(\d{3})\d\d(\d{2})/";
    $replacement = "\$1****\$3";
    return preg_replace($pattern, $replacement, $phone);
}

//手机号、固话验证
function phoneCheck($phone)
{
    $isMob = "/^1[3-9]{1}[0-9]{9}$/";
    $isTel = "/^([0-9]{3,4}-)?[0-9]{7,8}$/";
    if (!preg_match($isMob, $phone) && !preg_match($isTel, $phone)) {
        return false;
    }
    return true;
}

function dumpSql()
{
    \DB::listen(function ($query) {
        $bindings = $query->bindings;
        $sql = $query->sql;
        foreach ($bindings as $replace) {
            $value = is_numeric($replace) ? $replace : "'" . $replace . "'";
            $sql = preg_replace('/\?/', $value, $sql, 1);
        }
        dump($sql);
    });
}

/**
 * 随机生成要求位数个字符
 * @param integer $length 规定几位字符
 */
function getRandChar($length)
{
    $str = null;
    $strPol = "ABCDEFGHJKLMNPQRSTUVWXYZ0123456789";//大小写字母以及数字
    $max = strlen($strPol) - 1;
    for ($i = 0; $i < $length; $i++) {
        $str .= $strPol[rand(0, $max)];
    }
    return $str;
}

//图片链接返回处理
function getImageLink($image)
{
    if (!strstr($image, 'http')) {
        $disk = config('admin.upload.disk');
        return empty($image) ? '' : Storage::disk($disk)->url($image);
    } else {
        return $image;
    }

}

//处理图片显示
function showImages($value)
{
    $data = json_decode($value, true);
    $str_img = '';
    $str_pdf = '';
    foreach ($data as $k => $v) {
        $disk = config('admin.upload.disk');
        $src = Storage::disk($disk)->url($v['path']);
        $arr = explode('.', $v['path']);
        if (strtolower($arr[1]) == 'pdf') {
            $str_pdf .= '
                <a href="' . $src . '" target="_blank" style="margin: 20px;">
                    <p><img style="width:100px;" src="/img/pdf.jpg" alt=""></p>
                    <p>' . $v['name'] . '</p>
                </a>';
        } else {
            $str_img .= '<img style="width:300px;margin:20px;" src="' . $src . '"/>';
        }
    }
    return $str_img . $str_pdf;
}

//地区处理
function areaCode($area)
{
    $data = json_decode(json_encode($area), true);
    //2022年3月9日17点57分 优化多级选择

    if ($data['district'] ?? 0) {
        $area_code = $data['district'];
    } elseif ($data['city'] ?? 0) {
        $area_code = $data['city'];
    } elseif ($data['province'] ?? 0) {
        $area_code = $data['province'];
    } else {
        $area_code = 0;
    }
    return $area_code;
}


/**
 * get请求
 *
 */
function curlGet($url,$query='')
{
    $url =  $query?$url .= '?' . http_build_query($query):$url;
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;
}

//curlpost
/**
 * post 请求
 * @param  string           $url        请求地址
 * @param  array|string     $data       请求参数
 * @param  integer          $timeOut    超时秒数
 * @return array
 */
 function curlPost($url, $data, $header=[])
{
    if (is_array($data)){
        $data = json_encode($data,JSON_UNESCAPED_UNICODE);
    }
    //启动一个CURL会话
    $ch = curl_init();
    // 设置curl允许执行的最长秒数
  //  curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //发送一个常规的POST请求。
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    //要传送的所有数据
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    // 执行操作
    curl_setopt($ch,CURLOPT_HTTPHEADER,$header);

    $res = curl_exec($ch);
    curl_getinfo($ch, CURLINFO_HTTP_CODE);
    return json_decode($res, true);
}

//百度转高德经纬度
function baiduGpsToGaodeGps(&$bd_lng, &$bd_lat)
{
    if(!is_numeric($bd_lng) || !is_numeric($bd_lat)){
        $bd_lng = $bd_lat = null;
    }
    $x_pi = pi() * 3000.0 / 180.0;
    $x = $bd_lng - 0.0065;
    $y = $bd_lat - 0.006;
    $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi);
    $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi);
    $bd_lng = $z * cos($theta);
    $bd_lat = $z * sin($theta);
}

//高德坐标转百度（传入经度、纬度）
function GaodeGpsToBaiduGps(&$gg_lng, &$gg_lat)
{
    $x_pi = pi() * 3000.0 / 180.0;
    $x = $gg_lng;
    $y = $gg_lat;
    $z = sqrt($x * $x + $y * $y) + 0.00002 * sin($y * $x_pi);
    $theta = atan2($y, $x) + 0.000003 * cos($x * $x_pi);
    $gg_lng = $z * cos($theta) + 0.0065;
    $gg_lat = $z * sin($theta) + 0.006;

}

function getFilePath($key)
{
    return $key ? config('filesystems.disks.qiniu.url') . $key : '';
}

function changeArr($ids)
{
    if (is_int($ids) || is_string($ids)) {
        $ids = explode(',', $ids);
    }
    return $ids;
}

//详情通过连接跳转
function detialsLink($url, $value)
{
    return '<a href="' . admin_url($url) . '">' . phoneHide($value) . '</a>';
}
function uriParams($uri)
{
    $parts = parse_url($uri);
    $query = isset($parts['query']) ? $parts['query'] : '';
    $params = [];
    parse_str($query,$params);
    return $params;
    // 将查询字符串分割成单独的参数字符串
    $queryParams = explode('&', $query);
    // 初始化最终的参数数组
    $params = [];
    // 解析每个参数并将其放入数组中
    foreach ($queryParams as $param) {
        list($name, $value) = explode('=', $param);
        $params[urldecode($name)] = urldecode($value);
    }
    return $params;
}
